package cn.hxlc.tie.demo.modular.zr.equipmentzr.service.impl;/*
 @author rui
 @date 2021/6/29 --11:15
*/

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.util.ObjectUtil;
import cn.hxlc.tie.demo.core.enums.StatusEnum;
import cn.hxlc.tie.demo.error.MdmExceptionEnum;
import cn.hxlc.tie.demo.modular.zr.equipmentzr.entity.Equipment;
import cn.hxlc.tie.demo.modular.zr.equipmentzr.entity.EquipmentGroup;
import cn.hxlc.tie.demo.modular.zr.equipmentzr.mapper.EquipmentGroupMapper;
import cn.hxlc.tie.demo.modular.zr.equipmentzr.mapper.EquipmentMapper;
import cn.hxlc.tie.demo.modular.zr.equipmentzr.model.param.EquipmentGroupParam;
import cn.hxlc.tie.demo.modular.zr.equipmentzr.model.result.EquipmentGroupResult;
import cn.hxlc.tie.demo.modular.zr.equipmentzr.service.EquipmentGroupService;
import cn.hxlc.tie.libs.mp.page.PageFactory;
import cn.hxlc.tie.model.exp.ServiceException;
import cn.hxlc.tie.model.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.*;

import static cn.hxlc.tie.demo.core.constant.TreeConst.ROOT_ID;
import static cn.hxlc.tie.demo.core.constant.TreeConst.ROOT_PID;

@Service
public class EquipmentGroupServiceImpl extends ServiceImpl<EquipmentGroupMapper, EquipmentGroup> implements EquipmentGroupService {

    @Autowired
    private EquipmentGroupMapper equipmentGroupMapper;
    @Autowired
    private EquipmentMapper equipmentMapper;

    private EquipmentGroup getEntity(EquipmentGroupParam equipmentGroupParam) {
        EquipmentGroup entity = new EquipmentGroup();
        BeanUtil.copyProperties(equipmentGroupParam, entity);
        return entity;
    }

    private Serializable getKey(EquipmentGroupParam param) {
        return param.getFid();
    }
    private boolean hasEntry(long fid){
     QueryWrapper<EquipmentGroup> twrapper=new QueryWrapper<>();
     twrapper.eq("fparentid",fid);
     Integer tcount= this.baseMapper.selectCount(twrapper);
     QueryWrapper<Equipment> gwrapper=new QueryWrapper<>();
     gwrapper.eq("fgroupid",fid);
     Integer gcount=equipmentMapper.selectCount(gwrapper);
        return tcount <= 0 && gcount <= 0;

    }
    @Override
    public List<Map<String, Object>> queryTree() {
        List<Map<String, Object>> treeMaps =
                this.listMaps(new QueryWrapper<EquipmentGroup>().select("fid as nodeId", "fname as nodeName", "fparentid as nodePid")
                );
        //装配根节点
        Map<String, Object> rootNode = MapBuilder.create(new HashMap<String, Object>())
                .put("nodeId", ROOT_ID)
                .put("nodeName", "设备分组")
                .put("nodePid", ROOT_PID).build();
        treeMaps.add(rootNode);
        return treeMaps;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean add(EquipmentGroupParam equipmentGroupParam) {
        EquipmentGroup entity = getEntity(equipmentGroupParam);
        if (ObjectUtil.isNull(entity.getFparentid())) {
            entity.setFparentid(ROOT_ID);
        }
        entity.setFcreatetime(new Date());
        entity.setFstatus(StatusEnum.AUX);
        //entity.getFcreatorid(UserUtil.getUserid());
        return save(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(EquipmentGroupParam param) {

        //NumberUtil.checkRepeatNumber(this.baseMapper,param.getFnumber(),param.getFid());
        EquipmentGroup oldEntity = getOldEntity(param);
        EquipmentGroup newEntity = getEntity(param);
        BeanUtil.copyProperties(newEntity, oldEntity);
         this.baseMapper.updateById(newEntity);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(EquipmentGroupParam equipmentGroupParam) {
        //存在子节点，删除失败
        Integer countGroup = baseMapper.selectCount(new LambdaQueryWrapper<EquipmentGroup>().eq(EquipmentGroup::getFparentid, equipmentGroupParam.getFid()).ne(EquipmentGroup::getFstatus, StatusEnum.DELETED));
        if (countGroup > 0) {
            throw new ServiceException(MdmExceptionEnum.CHILDEXIST_ERROR.getCode(), MdmExceptionEnum.CHILDEXIST_ERROR.getMessage());
        }

        return removeById(equipmentGroupParam.getFid());
    }

    @Override
    public EquipmentGroup getGroupById(Long fid) {
        return baseMapper.selectById(fid);
    }

    @Override
    public PageResult<EquipmentGroupResult> queryListOne(EquipmentGroupParam equipmentGroupParam) {
        Page<EquipmentGroupResult> equipmentGroupPage= PageFactory.defaultPage();
        IPage<EquipmentGroupResult> resultPage= equipmentGroupMapper.queryListOne(equipmentGroupPage,equipmentGroupParam);
        return new PageResult<>(resultPage);
    }

    private EquipmentGroup getOldEntity(EquipmentGroupParam param) {
        return this.getById(getKey(param));
    }


}




